//
//  LDBase2ChoseJobDetailsVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/26.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
let sendChose2TitleIDAndTitle = "sendChose2TitleIDAndTitle"

<<<<<<< HEAD
class LDBase2ChoseJobDetailsVC: UIViewController {
=======
class LDBase2ChoseJobDetailsVC: LDBaseCustomizeVC {
>>>>>>> dingdong2.1.0

    var chose2TitleArr = [String]()
    var chose2TitleArr_ID = [String]()
    var paramsArr:Array<NSDictionary>?
    
    var chose3TitleArr = [String]()
    
    var tableViewLeft:UITableView?
    var tableViewRight:UIScrollView?
    
    let tableViewLeftW:CGFloat = 100
    let tableViewLeftH:CGFloat = 80
    
    var leftTableviewLab:UILabel?
    //改变颜色
    var preCell:UITableViewCell?
<<<<<<< HEAD
=======
    
>>>>>>> dingdong2.1.0
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefaultContent()
        setupSelect()
    }
    
    func setNoti(dict:Dictionary<String,AnyObject>) {
        
        NSNotificationCenter.defaultCenter().postNotificationName(sendChose2TitleIDAndTitle, object: self, userInfo: dict)
    }
<<<<<<< HEAD
=======
    
>>>>>>> dingdong2.1.0
    func setDefaultContent() {
        
        tableViewLeft = LDBaseTableView.init(frame: CGRectMake(0, 0, tableViewLeftW, LDFrame.ScreenHeight), style: .Plain)
        tableViewLeft?.backgroundColor = LDGolbalColor.TagBgColor
        tableViewLeft?.contentInset = UIEdgeInsetsMake(0, 0, LDFrame.NavH, 0)
        tableViewLeft?.dataSource = self
        tableViewLeft?.delegate = self
        
        tableViewRight = UIScrollView.init(frame: CGRectMake(tableViewLeftW, 0, LDFrame.ScreenWidth - 100, LDFrame.ScreenHeight))
        tableViewRight?.backgroundColor = LDGolbalColor.White
<<<<<<< HEAD
        view.addSubview(tableViewLeft!)
        view.addSubview(tableViewRight!)
=======
        
        view.addSubview(tableViewLeft!)
        view.addSubview(tableViewRight!)
        
        LDCheckPublicInfo.shareInstance.checkoutAllJobCategory { [weak self](data) in
            
            self?.updateView(data)
//            print("")
            
        }
    }
    
    func updateView(paramsArr:[NSDictionary]) {
        
       let model = baseChoseJobKindsCellModel.yy_modelWithDictionary(paramsArr[0] as [NSObject : AnyObject])
        chose2TitleArr = (model?.Level2Title)!
        chose2TitleArr_ID = (model?.Level2Title_ID)!
        self.paramsArr = model?.level2
        title = model?.level1_name
        tableViewLeft?.reloadData()
//        navigationController?.pushViewController(vc, animated: true)
>>>>>>> dingdong2.1.0
    }
    
    func setupSelect() {
        
        let index = NSIndexPath.init(forRow: 0, inSection: 0)
        tableViewLeft?.selectRowAtIndexPath(index, animated: false, scrollPosition: UITableViewScrollPosition.None)
    }
    
   
}
extension LDBase2ChoseJobDetailsVC:UITableViewDelegate,UITableViewDataSource {
    
    //delegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
<<<<<<< HEAD
        return chose2TitleArr.count
=======
        if chose2TitleArr.count > 0 {
            
            return chose2TitleArr.count
        } else {
            
            return 0
        }
        
>>>>>>> dingdong2.1.0
        
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = creatLeftCell(tableView, indexPath: indexPath)
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        changeCellStatus(tableView, indexPath: indexPath)
        change3ViewContent(tableView, index: indexPath)
        
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 45
    }
    
    //显示detail 3
    func change3ViewContent(tableView:UITableView,index:NSIndexPath) {
        
        let level2_id = chose2TitleArr_ID[index.row]
<<<<<<< HEAD
        setNoti(["level2_id":level2_id])
=======
        let level2_name = chose2TitleArr[index.row]
        
        setNoti(["level2_id":level2_id,"level2_name":level2_name])
>>>>>>> dingdong2.1.0
        
        for item in (tableViewRight?.subviews)! {
            item.removeFromSuperview()
            
        }
        var titleArr = [String]()
        var title_IdArr = [String]()
        
        let selArr =  paramsArr![index.row]
        let level3 = selArr["level3"] as! Array<Dictionary<String,String>>
        
        for dict in level3 {
            let title = dict["level3_name"]
            
            titleArr.append(title!)
            title_IdArr.append(dict["level3_id"]!)
        }
        
        let detialsViews = LDBaseTagsView.init(frame: tableViewRight!.bounds)
        detialsViews.control = "LDBase2ChoseJobDetailsVC"
        
        if tableView.cellForRowAtIndexPath(index)?.center != nil {
            detialsViews.anchorPoint = CGPointMake(0, (tableView.cellForRowAtIndexPath(index)?.center.y)!)
        }
        
        detialsViews.titleParams = titleArr
        detialsViews.title_id = title_IdArr
        
        self.tableViewRight?.addSubview(detialsViews)
    }
    
    
    func changeCellStatus(tableView:UITableView,indexPath:NSIndexPath) {
        
//        let level2_id = chose2TitleArr_ID[indexPath.row]
//        setNoti(["level2_id":level2_id])
        preCell?.backgroundColor = LDGolbalColor.TagBgColor
        preCell?.textLabel?.textColor = LDFontColor.DeepGray
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.backgroundColor = LDGolbalColor.White
        cell?.textLabel?.textColor = LDFontColor.Green
        preCell = cell
    }
    
    
    func creatLeftCell(tableView:UITableView,indexPath:NSIndexPath) -> UITableViewCell{
        
        var cell = tableView.dequeueReusableCellWithIdentifier("cell2chose")
        if cell == nil {
            
            cell = LDBaseCells.init(style: .Default, reuseIdentifier: "cell2chose")
            cell?.textLabel?.textAlignment = .Center
            cell?.textLabel?.textColor = LDFontColor.DeepGray
            cell?.textLabel?.font = LDFont.Font13
            cell?.textLabel?.numberOfLines = 0
            cell?.textLabel?.textAlignment = NSTextAlignment.Center
            cell?.textLabel?.text = chose2TitleArr[indexPath.row]
            cell?.backgroundColor = LDGolbalColor.TagBgColor
        }
        if indexPath.row == 0 {
            
            preCell = cell
            change3ViewContent(tableView, index: indexPath)
            preCell?.backgroundColor = LDGolbalColor.White
            preCell?.textLabel?.textColor = LDFontColor.Green

        }
        return cell!
    }
}
